package com.coolwen.service;

import com.coolwen.dao.TeamMapper;
import com.coolwen.dao.UserTeamMapper;
import com.coolwen.entity.SccocerException;
import com.coolwen.entity.Team;
import com.coolwen.entity.UserTeam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zero on 2016/12/6.
 */
@Service("teamService")
public class TeamServiceImpl implements TeamService {


    @Autowired
    TeamMapper teamMapper;


    @Autowired
    UserTeamMapper userTeamMapper;


    /**
     * 通过 teamId查询team
     * @param teamId
     * @return
     */

    @Override
    public Team findTeamById(Integer teamId) {
        return teamMapper.selectByPrimaryKey(teamId);
    }


    /**
     * 添加一个球队
     * @param team
     * @return
     */

    @Override
    public int addTeam(Team team) {


        if (userTeamMapper.selectUserTeamByUserID(team.getTeamId()) != null) {
            throw new SccocerException("已经创建球队");
        }
        teamMapper.insertSelective(team);
        Integer id = team.getTeamId();
        return id;
    }

    /**
     * 把 teamId和 userId放入team_user表中
     * @param team
     * @param userId
     * @return
     */

    @Override
    public int addTeam(Team team, Integer userId) {


        Integer teamId = this.addTeam(team);
        UserTeam userTeam = new UserTeam();
        System.out.println("teamId----->>>>>" + teamId);
        userTeam.setTeamId(teamId);

        userTeam.setUserId(userId);
        return userTeamMapper.insertSelective(userTeam);

    }

    /**
     * 返回修改球队内容
     * @param team
     * @param teamId
     * @return
     */

    @Override
    public int updateTeam(Team team, Integer teamId) {
        Team team1=findTeamById(teamId);
        team1.setTeamSummary(team.getTeamSummary());
        team1.setTeamName(team.getTeamName());
        team1.setTeamLeader(team.getTeamLeader());
        team1.setTeamCaptain(team.getTeamCaptain());
        team1.setTeamArea(team.getTeamArea());
        return teamMapper.updateByPrimaryKeySelective(team1);
    }

    /**
     * 查询所有Team
     * @return
     */

    @Override
    public List<Team> findAllTeam() {
        return teamMapper.selectAllTeam();
    }


    /**
     * 计算所有队员人数
     * @param teamId
     * @return
     */

    @Override
    public int CountMemberByTeamId(Integer teamId) {
        return teamMapper.countMemberByTeamId(teamId);
    }

    /**
     * 计算约战次数
     * @param teamId
     * @return
     */


    @Override
    public int CountChallengeByTeamId(Integer teamId) {
        return teamMapper.countChallengeByTeamId(teamId);
    }


    /**
     * 通过主场Id查询胜负平场次
     * @param result
     * @param teamId
     * @return
     */


    @Override
    public int CountChallengeResultByTeamId(String result,Integer teamId) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("challengeResult", result);
        params.put("challengeTeamid", teamId);
        return teamMapper.selectResult(params);

    }


    /**
     * 通过客场Id查询胜负平场次
     * @param result
     * @param receiveId
     * @return
     */


    @Override
    public int CountChallengeResultByReceiveId(String result,Integer receiveId) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("challengeResult", result);
        params.put("challengeReceiveid", receiveId);
        return teamMapper.selectResult(params);

    }


    /**
     * 通过球队名字查询球队
     * @param teamName
     * @return
     */

    @Override
    public List<Team> findTeamByTeamName(String teamName) {
        return teamMapper.selectTeamByTeamName(teamName);
    }


}
